package 每日一题;

import java.util.Arrays;

/**
 * @author 会玩的洋洋
 * https://leetcode-cn.com/problems/minimum-difference-between-highest-and-lowest-of-k-scores/
 */
public class _1984_学生分数的最小差值 {
    /**
     * 排序 + 滑动窗口
     * @param nums 目标数组
     * @param k 取得的数量
     * @return int
     */
    public int minimumDifference(int[] nums, int k) {
        if (k == 1) {
            return 0;
        }
        Arrays.sort(nums);
        int min = Integer.MAX_VALUE;
        for (int i = 0; i <= nums.length - k; i++) {
            min = Math.min(nums[i + k - 1] - nums[i], min);
        }
        return min;
    }
}
